package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加菜品或套餐到购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
//        保存当前操作的用户
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

//        查询当前菜品或套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);

        if(dishId!=null){
//            添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else{
            //        添加到物购车的是套餐
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
//        查询当前菜品或套餐是否在购物车中
        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);
//        如果已经存在，就在原来数量上加1
        if(cartServiceOne!=null){
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }
        else{
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }
//        如果不存在，则添加到购物车，数量默认是1
        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车,根据用户id
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        lqw.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
//        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
//        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.clean();
        return R.success("购物车已清空");
    }

    /**
     * 减少菜品或套餐数量
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
//        先判断减少的是菜品还是套餐
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if(dishId!=null){
//            减少菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
            ShoppingCart serviceOne = shoppingCartService.getOne(lqw);
            Integer number = serviceOne.getNumber();
            serviceOne.setNumber(number-1);
            Integer lastNumber = serviceOne.getNumber();
            if(lastNumber>0){
//                对数据进行更新操作
                shoppingCartService.updateById(serviceOne);
            }else if(lastNumber==0){
                shoppingCartService.removeById(serviceOne.getId());
            }else if(lastNumber<0){
                return R.error("数据异常");
            }
            return R.success(serviceOne);
        }
//        减少套餐
        lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(lqw);
        Integer cartNumber = cart.getNumber();
        cart.setNumber(cartNumber-1);
        Integer lastNumber2 = cart.getNumber();
        if(lastNumber2>0){
            shoppingCartService.updateById(cart);
        }else if(lastNumber2==0){
            shoppingCartService.removeById(cart.getId());
        }else if(lastNumber2<0){
            return R.error("数据异常");
        }
        return R.success(cart);
    }
}
